<?php

namespace app\common\model\user;

use app\common\library\Yyou;
use think\Log;
use think\Model;
use traits\model\SoftDelete;

class Withdrawlog extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'user_withdrawlog';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    protected $url = 'https://demo-open.xinfushe.com';

    // 追加属性
    protected $append = [
        'status_text',
        'uptime_text',
        'createtime_text'
    ];



    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getUptimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['uptime']) ? $data['uptime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setUptimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function bank()
    {
        return $this->belongsTo('app\common\model\Bank', 'bank_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    protected static function init()
    {
        self::event('before_update', function ($obj) {
            if (array_key_exists('status', $obj->getChangedData())) {

                if ($obj->status == '2') {
                    //佣金回滚
                    $obj->user->commission += $obj->money;
                    $obj->user->save();
                    // 提现状态
                    // {{thing1.DATA}}

                    // 提现金额
                    // {{amount2.DATA}}

                    // 提现时间
                    // {{date4.DATA}}

                    // 审核结果
                    // {{phrase15.DATA}}

                    // 提现说明
                    // {{thing5.DATA}}
                    $s = sendSubTemplateMsg(
                        $obj->user,
                        "3kgI-b9n4aE9QN30lrnY9ETQAOb4Z_CoR5uOOYqcFuc",
                        [
                            'thing1' => ['value' => '审核未通过'],
                            'amount2' => ['value' => $obj->money ?? 0],
                            'date4' => ['value' => date('Y-m-d H:i', $obj->createtime)],
                            'phrase15' => ['value' => '未通过'],
                            'thing5' => ['value' => $obj['msg'] ?? '无'],
                        ]
                    );
                    Log::error('订阅消息2' . var_export($s, 1));
                }
                if ($obj->status == '1') {
                    //设置时间
                    $obj->uptime = time();
                    if($obj->bourEmpNo){
                        $acceptance = (new Yyou([
                            'bourEmpNo' => $obj->bourEmpNo,
                        ]))->acceptanceTaskJson();
                        if ($acceptance['code'] !=  '000000') {
                            exception($acceptance['mesg']);
                        }
                    }
                    // $s = sendSubTemplateMsg(
                    //     $obj->user,
                    //     "3kgI-b9n4aE9QN30lrnY9ETQAOb4Z_CoR5uOOYqcFuc",
                    //     [
                    //         'thing1' => ['value' => '提现金额已转账至您的银行卡，敬请查收'],
                    //         'amount2' => ['value' => $obj->money ?? 0],
                    //         'date4' => ['value' => date('Y-m-d H:i', $obj->createtime)],
                    //         'phrase15' => ['value' => '已通过'],
                    //         'thing5' => ['value' => '到账有一定延迟，请耐心等待处理'],
                    //     ]
                    // );
                    // Log::error('订阅消息3' . var_export($s, 1));
                }
            }
        });
    }

    public function sign($params)
    {
        uksort($params, 'strcmp');

        $str = '';
        foreach ($params as $key => $val) {
            $str .= "{$key}={$val},";
        }

        $sign = md5(urlencode($str . "secret=" . $this->appSecret));

        return $sign;
    }
    public function send($params)
    {
        $clien = new \GuzzleHttp\Client();
        $params['sign'] = $this->sign($params);
        $url = $this->url . '/api/R_ACCOUNT_02';
        $res = $clien->post($url,  $params);
        return $res;
    }
}
